import configDB as Mysql_Db
import public_method as pub
import time,datetime
import re
v4_sqlMapper=Mysql_Db.Mysql('DATABASE_2')
v5_sqlMapper=Mysql_Db.Mysql('DATABASE_1')

# v4数据库名
old_database = v4_sqlMapper.get_one(' select database() ;')
# v5数据库名
new_database = v5_sqlMapper.get_one(' select database() ;')
# 查询所有已经转换完成的站点数据
webList=v5_sqlMapper.get_all("select id,new_table,old_table,new_id,old_id from data_conversion_info where new_table='public_web_config' order by id; ")
# 加载所有转换过的用户id信息
userList=v5_sqlMapper.get_all("select old_id,new_id from data_conversion_info where new_table='public_user';")
# 词库数据 (专业名词、敏感词、停止词、启类词、定制分词、同义词、枚举词、关联词、 词权重)
wordList=v4_sqlMapper.get_all("select max(id) id,word,max(ifnull(score,0)) score,tyc,max(ifnull(type,0)) type,wodmode,webId,max(addUserId) addUserId,concat(max(add_time),'') add_time from worddoc  where  wodMode in(1,2,4,5,6,7,8,10,13) and del=0  group by word,webId,tyc,wodmode,webId,ifnull(score,0) union all " \
                            +" select max(id) id,words,0 as score,null as tyc,0 as type, 3 as wodMode, webId,max(addUserId) addUserId,max(concat(time,'')) time from brunchwords where del=0 group by words,webId ;")

# wx图文新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
wxappMap=dict()
# 将结果数组转化为字典键值对供后续新旧用户对应使用,K为v4ID值,V为v5对应新ID值
userMap=dict(userList)
# 小渠道新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
sourceMap=dict()
# 存放v4大小渠道对应值K于主键ID的对应V
sourceIdMap=dict()
# 分类新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
classMap=dict()
# 标签新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
lableMap=dict()
# 问题新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
questionMap=dict()
# 问题配置表solution新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
solutionMap=dict()
# 知识点新旧对应值存放集合,K为v4ID值,V为v5对应新ID值
questionMap=dict()
# 素材对应新旧值存放集合,,K为v4ID值,V为v5对应新ID值
materialMap=dict()
# 素材路径对应地址集合,,K为v4ID值,V为path
pathlMap=dict()
wxpathMap=dict()
# 存放对应词k词汇，v id
wordMap=dict()
print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
for webOne in webList:
    data_id=webOne[0]
    v5_table=webOne[1]
    v4_table=webOne[2]
    v5_webId=webOne[3]
    v4_webId=webOne[4]
    mainUid=v5_sqlMapper.get_one("select id from public_user where web_id="+str(v5_webId)+" and type=1;")
    if mainUid == None:
        mainUid='null'
    class_num = 0
    lable_num=0
    channel_num=0
    # 素材数据遍历转换,需转移素材文件至v5服务器中
    # 词汇数据转换 id,word,score,tyc,type,wodMode,webId,addUserId,concat(add_time,'') add_time
    for wordOne in wordList:
        word_id=wordOne[0]
        word_content=wordOne[1] if wordOne[1] != None else ''
        word_score=wordOne[2]
        word_tyc=str(wordOne[3]) if wordOne[3] != None else ''
        word_type=wordOne[4]
        word_mode=wordOne[5]
        word_webId=wordOne[6]
        word_addUserId=wordOne[7]
        word_addTime=pub.str_symbol(wordOne[8]) if wordOne[8] != None else 'sysdate()'
        if word_webId == v4_webId:
            # 专业名词(4)、敏感词(10)、停止词(1)、启类词(13)、定制分词(8)、同义词(2)、枚举词(7)、关联词(6)、 词权重(5)、主干词(3)
            # 专业名词
            if word_mode == 4:
                sqls="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                    +"values("+pub.str_symbol(word_content[0:100])+","+str(word_score)+",0,1,0,0,0,"+str(v5_webId)+",0,"+str(userMap.get(word_addUserId) if userMap.get(word_addUserId) !=None else mainUid)+","+word_addTime+")"

            # 敏感词
            if word_mode == 10:
                sqls="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                    +"values("+pub.str_symbol(word_content[0:100])+","+str(word_score)+",0,2,0,0,0,"+str(v5_webId)+",0,"+str(userMap.get(word_addUserId) if userMap.get(word_addUserId) !=None else mainUid)+","+word_addTime+")"

            # 停止词
            if word_mode == 1:
                sqls="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                    +"values(" + pub.str_symbol(word_content[0:100]) + "," + str(word_score) + ",0,3,0,0,0," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

            # 启类词
            if word_mode == 13:
                sqls ="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                     +"values(" + pub.str_symbol(word_content[0:100]) + "," + str(word_score) + ",0,1,0,0,1," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

            # 主干词
            if word_mode == 3:
                sqls ="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                     +"values(" + pub.str_symbol(word_content[0:100]) + "," + str(word_score) + ",0,1,0,1,0," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

            # 同义词
            if word_mode == 2:
                sqls ="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                     +"values(" + pub.str_symbol(word_content[0:100]) + "," + str(word_score) + ",0,1,0,0,0," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

            # 枚举词
            if word_mode == 7:
                sqls = "insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                     + "values(" + pub.str_symbol(word_content[0:100]) + "," + str(word_score) + ",0,1,0,0,0," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

            # 关联词
            if word_mode == 6 and len(word_tyc) > 0:

                word_tyc1 = str(word_tyc).split(',')
                for w in range(len(word_tyc1)):
                    if w > 0:
                        break
                    wstr=word_tyc1[w]
                    # 防止数据空
                    if wstr == None and w == 0:
                        w=w-1
                        continue
                    sqls ="insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                         +"values(" + pub.str_symbol(wstr[0:100]) + ",0,0,1,0,0,0," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"
            # 词权重
            if word_mode == 5:
                sqls = "insert into kn_word_doc(word,weight,nature,type,nominal,principal_flag,open_flag,web_id,del,create_user_id,create_time) " \
                       + "values(" + pub.str_symbol(word_content[0:100]) + "," + str(word_score) + ",0,1,0,0,0," + str(v5_webId) + ",0," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

            if word_content != None and  word_mode not in(1,10):
                wordKey=pub.str_symbol(word_content[0:100] + str(1)+str(v4_webId))
                if wordMap.get(wordKey) == None:
                    print(sqls)
                    # 将处理好的sql语句进行执行
                    new_wordId = v5_sqlMapper.insert_id(sqls)
                    wordMap[wordKey]=new_wordId
                    v5_sqlMapper.insert(pub.insert_conversion(new_wordId, word_id, 'kn_word_doc', 'worddoc', new_database,old_database))

            # 判断是否存在子句
            if  len(word_tyc) > 0:
                word_tyc = str(word_tyc).split(',')
                # 循环添加副词
                for w in range(len(word_tyc)):
                    words=word_tyc[w]
                    # 处理同义词
                    if words != None and word_mode == 2:
                        sql_s="insert into kn_word_relation(word_id,word,type,multi_dimension_id,web_id,create_user_id,create_time) " \
                             +"values("+str(new_wordId)+","+pub.str_symbol(words[0:100])+",1,0,"+str(v5_webId)+","+str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime +")"

                    # 处理关联词
                    elif words != None and word_mode == 6:
                        # 对第一个单词做出处理动作
                        if w == 0:
                            sql_str = "insert into kn_word_group(weight,web_id,del,create_user_id,create_time) " \
                                    + "values("+str(word_score)+","+str(v5_webId)+",0,"+str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid)+","+word_addTime+")"
                            print(sql_str)
                            new_wordGroupId=v5_sqlMapper.insert_id(sql_str)
                            v5_sqlMapper.insert(pub.insert_conversion(new_wordGroupId, word_id, 'kn_word_relation', 'worddoc',new_database, old_database))

                            sql_s = "insert into kn_word_relation(word_id,word,type,multi_dimension_id,web_id,create_user_id,create_time) " \
                                  + "values("+str(new_wordGroupId)+","+pub.str_symbol(wstr[0:100])+",4,0,"+str(v5_webId)+","+str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime +")"
                        else:
                            sql_s = "insert into kn_word_relation(word_id,word,type,multi_dimension_id,web_id,create_user_id,create_time) " \
                                  + "values(" + str(new_wordGroupId) + "," + pub.str_symbol(words[0:100]) + ",4,0," + str(v5_webId)+"," + str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime + ")"

                    elif words != None and word_mode == 7:
                        sql_s="insert into kn_word_relation(word_id,word,type,multi_dimension_id,web_id,create_user_id,create_time) " \
                             +"values("+str(new_wordId)+","+pub.str_symbol(words[0:100])+",3,0,"+str(v5_webId)+","+str(userMap.get(word_addUserId) if userMap.get(word_addUserId) != None else mainUid) + "," + word_addTime +")"

                    else:
                        continue
                    print(sql_s)
                    new_relationId = v5_sqlMapper.insert_id(sql_s)
                    v5_sqlMapper.insert(pub.insert_conversion(new_relationId, word_id, 'kn_word_relation', 'worddoc', new_database,old_database))








print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))